package com.hub.frame.config;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.core.injector.methods.Insert;
import com.baomidou.mybatisplus.core.injector.methods.Update;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.extension.injector.methods.*;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Configuration
@MapperScan("com.hub.mapper")
public class MybatisPlusConfiguration {

    /**
     * mybatis-plus SQL执行效率插件[生产环境可以关闭]
     * 设置环境开启
     */
    @Bean
    @Profile({"dev", "test"})
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor().setFormat(true).setMaxTime(10000);
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 乐观锁
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * 逻辑删除
     */
    @Bean
    public ISqlInjector iSqlInjector() {
        return new AbstractSqlInjector() {
            @Override
            public List<AbstractMethod> getMethodList() {
                return Stream.of(
                        new Insert(),
                        new LogicDelete(),
                        new LogicDeleteByMap(),
                        new LogicDeleteById(),
                        new LogicDeleteBatchByIds(),
                        new Update(),
                        new UpdateById(),
                        new LogicSelectById(),
                        new LogicSelectBatchByIds(),
                        new LogicSelectByMap(),
                        new LogicSelectOne(),
                        new LogicSelectCount(),
                        new LogicSelectMaps(),
                        new LogicSelectMapsPage(),
                        new LogicSelectObjs(),
                        new LogicSelectList(),
                        new LogicSelectPage()
                ).collect(Collectors.toList());
            }
        };
    }
}
